package com.wmlive.hhvideo.heihei.mainhome.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.main.ExplosionResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.mainhome.view.ExplosionView;
import com.wmlive.networklib.entity.DCNetObserver;

/**
 * Created by vhawk on 2017/6/1.
 * Modify by lsq
 */

public class ExplosionPresenter extends BasePresenter<ExplosionView> {

    private int offset;

    public ExplosionPresenter(ExplosionView view) {
        super(view);
    }


    public void explosionVideo(final boolean isRefresh) {
        String url = InitCatchData.opusListOpusByTime();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_EXPLOSION_VIDEO, getHttpApi().explosionVideo(url, offset = isRefresh ? 0 : offset))
                    .subscribe(new DCNetObserver<ExplosionResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ExplosionResponse response) {
                            offset = response.getOffset();
                            if (viewCallback != null) {
                                viewCallback.handleExplosionSucceed(isRefresh,
                                        response.getTimeOpusList(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_EXPLOSION_VIDEO : (HttpConstant.TYPE_EXPLOSION_VIDEO + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(isRefresh ? HttpConstant.TYPE_EXPLOSION_VIDEO : (HttpConstant.TYPE_EXPLOSION_VIDEO + 1));
        }

    }

}
